/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjectMgtSystem.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;
import ProjectMgtSystem.Bean.*;
import ProjectMgtSystem.Entities.*;

/**
 *
 * @author Nguyen Phu Cuong
 *  Create date: 2011/07/19
 */
public class AssignmentAction extends LookupDispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    /** Provides the mapping from resource key to method name.
     * @return Resource key / method name map.
     */
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("AssignmentAction.viewPartiProj", "viewPartiProj");
        map.put("AssignmentAction.viewTaskByProj", "viewTaskByProj");
        map.put("AssignmentAction.viewAssignInfo", "viewAssignInfo");
        map.put("AssignmentAction.updateAssignInfo", "updateAssignInfo");
        map.put("AssignmentAction.updateAssignEngineer", "updateAssignEngineer");
        map.put("AssignmentAction.addAssignEngineer", "addAssignEngineer");
        map.put("AssignmentAction.viewProjectProgress", "viewProjectProgress");
        map.put("AssignmentAction.viewTaskByProj1", "viewTaskByProj1");
        return map;
    }

    /** Action called on View Participated Project button click
     */
    public ActionForward viewPartiProj(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        AssignmentActionBean assActionFrm = (AssignmentActionBean) form;
        ArrayList<Project> arrPro = new ArrayList<Project>();
        request.setAttribute("USERID", assActionFrm.getUserid());

        arrPro = assActionFrm.viewPartiProj(assActionFrm.getUserid());
        if (arrPro == null) {
            return mapping.findForward("failure");

        } else {
            request.setAttribute("partiProjList", arrPro);

            return mapping.findForward("viewPartiProj");
        }
    }
    
    /**
     * View Project Progress Action
     */
    public ActionForward viewProjectProgress(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        
        AssignmentActionBean assActionFrm = (AssignmentActionBean) form;
        ArrayList<Project> arrPro = new ArrayList<Project>();
        request.setAttribute("USERID", assActionFrm.getUserid());

        arrPro = assActionFrm.viewProjectProgress(assActionFrm.getUserid());
        if (arrPro == null) {
            return mapping.findForward("failure");

        } else {
            request.setAttribute("partiProjList", arrPro);

            return mapping.findForward("viewProjProgress");
        }
    }

    /** Action called on Detail button click
     *  View task info by project id
     */
    public ActionForward viewTaskByProj(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        AssignmentActionBean assActionFrm = (AssignmentActionBean) form;
        ArrayList<Assignment> assList = assActionFrm.viewTaskByProj(assActionFrm.getProjectid());
        request.setAttribute("TaskInfoByProj", assList);
        request.setAttribute("PROJECTID", assActionFrm.getProjectid());
        request.setAttribute("USERID", assActionFrm.getUserid());
        return mapping.findForward("viewTaskInfoByProj");
    }
    
    /** Action called on Detail button click
     *  View task info by project id
     */
    public ActionForward viewTaskByProj1(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        AssignmentActionBean assActionFrm = (AssignmentActionBean) form;
        ArrayList<Assignment> assList = assActionFrm.viewTaskByProj(assActionFrm.getProjectid());
        request.setAttribute("TaskInfoByProj", assList);
        request.setAttribute("PROJECTID", assActionFrm.getProjectid());
        request.setAttribute("USERID", assActionFrm.getUserid());
        return mapping.findForward("viewTaskInfoByProj");
    }
    
    
    /** Action called on Assign Engineer button click
     */
    public ActionForward viewAssignInfo(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {
        AssignmentActionBean assActionFrm = (AssignmentActionBean) form;
        String userid = assActionFrm.getUserid();
        String projectid = assActionFrm.getProjectid();
        String taskid = assActionFrm.getTaskid();

        ArrayList<Assignment> assInfoList = new ArrayList<Assignment>();
        assInfoList = assActionFrm.viewAssignInfo(projectid, taskid);
        request.setAttribute("AssignInfoList", assInfoList);

        ArrayList<Engineer> assEngineerList = new ArrayList<Engineer>();
        assEngineerList = assActionFrm.viewEngineerAssignInfo(projectid, taskid);
        request.setAttribute("AssignEngineerList", assEngineerList);

        ArrayList<Engineer> enList = new ArrayList<Engineer>();
        enList = assActionFrm.listEngineerNotAssigned(userid, projectid, taskid);
        request.setAttribute("ListEnNotAssigned", enList);

        request.setAttribute("PROJECTID", projectid);
        request.setAttribute("TASKID", taskid);
        request.setAttribute("USERID", userid);
        return mapping.findForward("viewAssignInfo");
    }

    /** Action called on Delete button click -- But actual action is update Assigned Engineer to Task
     */
    public ActionForward updateAssignEngineer(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        AssignmentActionBean assActionFrm = (AssignmentActionBean) form;
        String projectid = assActionFrm.getProjectid();
        String taskid = assActionFrm.getTaskid();
        String userid = assActionFrm.getUserid();
        String engineerid = assActionFrm.getEngineerid();

        int row = assActionFrm.updateAssignEngineer(projectid, taskid, engineerid);
        if (row != 0) {
            ArrayList<Assignment> assInfoList = new ArrayList<Assignment>();
            assInfoList = assActionFrm.viewAssignInfo(projectid, taskid);
            request.setAttribute("AssignInfoList", assInfoList);

            ArrayList<Engineer> assEngineerList = new ArrayList<Engineer>();
            assEngineerList = assActionFrm.viewEngineerAssignInfo(projectid, taskid);
            request.setAttribute("AssignEngineerList", assEngineerList);

            ArrayList<Engineer> enList = new ArrayList<Engineer>();
            enList = assActionFrm.listEngineerNotAssigned(userid, projectid, taskid);
            request.setAttribute("ListEnNotAssigned", enList);

            request.setAttribute("TASKID", taskid);
            request.setAttribute("PROJECTID", projectid);
            request.setAttribute("USERID", userid);
            return mapping.findForward("viewAssignInfo");
        } else {
            return mapping.findForward("failure");
        }
    }

    /**
     * 
     */
    public ActionForward addAssignEngineer(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {

        AssignmentActionBean assActionFrm = (AssignmentActionBean) form;
        String projectid = assActionFrm.getProjectid();
        String taskid = assActionFrm.getTaskid();
        String userid = assActionFrm.getUserid();
        String engineerid = assActionFrm.getEngineerid1();

        int row = assActionFrm.addAssignEngineer(projectid, taskid, engineerid);
        if (row != 0) {
            ArrayList<Assignment> assInfoList = new ArrayList<Assignment>();
            assInfoList = assActionFrm.viewAssignInfo(projectid, taskid);
            request.setAttribute("AssignInfoList", assInfoList);

            ArrayList<Engineer> assEngineerList = new ArrayList<Engineer>();
            assEngineerList = assActionFrm.viewEngineerAssignInfo(projectid, taskid);
            request.setAttribute("AssignEngineerList", assEngineerList);

            ArrayList<Engineer> enList = new ArrayList<Engineer>();
            enList = assActionFrm.listEngineerNotAssigned(userid, projectid, taskid);
            request.setAttribute("ListEnNotAssigned", enList);

            request.setAttribute("TASKID", taskid);
            request.setAttribute("PROJECTID", projectid);
            request.setAttribute("USERID", userid);
            return mapping.findForward("viewAssignInfo");
        } else {
            return mapping.findForward("failure");
        }
    }

    /**
     * 
     */
    public ActionForward updateAssignInfo(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {

        AssignmentActionBean assActionFrm = (AssignmentActionBean) form;
        String projectid = assActionFrm.getProjectid();
        String taskid = assActionFrm.getTaskid();
        String userid = assActionFrm.getUserid();
        String planStartDate = assActionFrm.getPlanStartDate();
        String planEndDate = assActionFrm.getPlanEndDate();
        String startDate = assActionFrm.getStartDate();
        String endDate = assActionFrm.getEndDate();
        String completeRatio = assActionFrm.getCompleteRatio();

        String msg = assActionFrm.updateAssignInfo(projectid, taskid, planStartDate, planEndDate, startDate, endDate, completeRatio);

       
            ArrayList<Assignment> assInfoList = new ArrayList<Assignment>();
            assInfoList = assActionFrm.viewAssignInfo(projectid, taskid);
            request.setAttribute("AssignInfoList", assInfoList);

            ArrayList<Engineer> assEngineerList = new ArrayList<Engineer>();
            assEngineerList = assActionFrm.viewEngineerAssignInfo(projectid, taskid);
            request.setAttribute("AssignEngineerList", assEngineerList);

            ArrayList<Engineer> enList = new ArrayList<Engineer>();
            enList = assActionFrm.listEngineerNotAssigned(userid, projectid, taskid);
            request.setAttribute("ListEnNotAssigned", enList);

            request.setAttribute("TASKID", taskid);
            request.setAttribute("PROJECTID", projectid);
            request.setAttribute("USERID", userid);
            request.setAttribute("ErrMessage", msg);  
            return mapping.findForward("viewAssignInfo");
        
       
                  
        }
    
    /* And your JSP would have the following format for submit buttons:
    
    <html:form action="/test">
    <html:submit property="method">
    <bean:message key="button.add"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.edit"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.delete"/>
    </html:submit>
    </html:form>
     */
}
